import request from "../utils/request";

// 获取患者列表
export const getPatients = async (params = {}) => {
  try {
    const { page = 1, size = 10, status, keyword } = params;
    const queryParams = { page, size };
    if (status) queryParams.status = status;
    if (keyword) queryParams.keyword = keyword;
    const response = await request.get("/api/patients", queryParams);
    return response;
  } catch (error) {
    console.error("获取患者列表失败:", error);
    throw error;
  }
};
// 获取患者详情
export const getPatientDetail = async (patientId) => {
  try {
    const response = await request.get(`/api/patients/${patientId}`);
    return response;
  } catch (error) {
    console.error("获取患者详情失败:", error);
    throw error;
  }
};
// 添加患者
export const addPatient = async (patientData) => {
  try {
    const response = await request.post("/api/patients", patientData);
    return response;
  } catch (error) {
    console.error("添加患者失败:", error);
    throw error;
  }
};
// 更新患者信息
export const updatePatient = async (patientId, patientData) => {
  try {
    const response = await request.put(`/api/patients/${patientId}`, patientData);
    return response;
  } catch (error) {
    console.error("更新患者失败:", error);
    throw error;
  }
};
// 删除患者
export const deletePatient = async (patientId) => {
  try {
    const response = await request.delete(`/api/patients/${patientId}`);
    return response;
  } catch (error) {
    console.error("删除患者失败:", error);
    throw error;
  }
};
// 患者设备绑定
export const bindPatientDevice = async (bindData) => {
  try {
    const response = await request.post("/api/patients/bind", bindData);
    return response;
  } catch (error) {
    console.error("患者设备绑定失败:", error);
    throw error;
  }
};
// 解除患者设备绑定
export const unbindPatientDevice = async (patientId) => {
  try {
    const response = await request.delete(`/api/patients/${patientId}/bind`);
    return response;
  } catch (error) {
    console.error("解除患者设备绑定失败:", error);
    throw error;
  }
};
// 获取患者设备绑定状态
export const getPatientDeviceBinding = async (patientId) => {
  try {
    const response = await request.get(`/api/patients/${patientId}/binding`);
    return response;
  } catch (error) {
    console.error("获取患者设备绑定状态失败:", error);
    throw error;
  }
};
//获取所有患者设备绑定
export const getAllPatientDeviceBindings = async () => {
  try {
    const response = await request.get("/api/patients/bindings");
    return response;
  } catch (error) {
    console.error("获取所有患者设备绑定失败:", error);
    throw error;
  }
};
// 搜索患者
export const searchPatients = async (keyword) => {
  try {
    const response = await request.get("/api/patients/search", { keyword });
    return response;
  } catch (error) {
    console.error("搜索患者失败:", error);
    throw error;
  }
};
